package org.zuel.boot.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zuel.boot.demo.entity.ChartData;
import org.zuel.boot.demo.service.IChartService;

import java.util.*;

@Api(tags = "管理")
@RestController
@RequestMapping("/zuel")
public class ChartController {

    @Autowired
    private IChartService chartService;

    @ApiOperation("行业分布")
    @GetMapping("/chart/chart1")
    public List<ChartData> selectIndustryData() {
        return chartService.selectIndustryData();
    }

    @ApiOperation("城市分布")
    @GetMapping("/chart/chart2")
    public Map<String, Object> selectCityData() {
        List<ChartData> rawList = chartService.selectCityData();

        List<String> nameList = new ArrayList<>();
        List<Double> valueList = new ArrayList<>();

        double otherTotal = 0.0;
        for (int i = 0; i < rawList.size(); i++) {
            ChartData data = rawList.get(i);
            if (i < 6) {
                nameList.add(data.getName());
                valueList.add(data.getValue());
            } else {
                otherTotal += data.getValue();
            }
        }

        if (otherTotal > 0) {
            nameList.add("其他地区");
            valueList.add(otherTotal);
        }

        Map<String, Object> response = new HashMap<>();
        response.put("name", nameList);
        response.put("value", valueList);
        return response;
    }

    @ApiOperation("行业就业人数增长")
    @GetMapping("/chart/chart3")
    public Map<String, Object> selectIndustryGrowth() {
        List<ChartData> rawData = chartService.selectIndustryGrowth();

        // 准备年份列表
        List<String> years = Arrays.asList("2019", "2020", "2021", "2022", "2023", "2024");

        // 用 Map<行业名, List<年增长值>> 来装数据
        Map<String, List<Double>> industryMap = new LinkedHashMap<>();

        // 中间统计每年的人数，Map<行业, Map<年, count>>
        Map<String, Map<String, Double>> tempMap = new HashMap<>();

        for (ChartData data : rawData) {
            String label = data.getLabel();
            String year = data.getName();
            Double count = data.getValue();

            tempMap.computeIfAbsent(label, k -> new HashMap<>()).put(year, count);
        }

        // 计算增长率： (今年人数 - 去年人数) / 去年人数 × 100%
        for (Map.Entry<String, Map<String, Double>> entry : tempMap.entrySet()) {
            String label = entry.getKey();
            Map<String, Double> yearCount = entry.getValue();
            List<Double> growthList = new ArrayList<>();

            for (int i = 0; i < years.size(); i++) {
                String year = years.get(i);
                if (i == 0) {
                    growthList.add(0.0); // 第一年的增长率设为 0
                } else {
                    String prevYear = years.get(i - 1);
                    double prev = yearCount.getOrDefault(prevYear, 0.0);
                    double curr = yearCount.getOrDefault(year, 0.0);
                    if (prev == 0) {
                        growthList.add(0.0); // 防止除零
                    } else {
                        double growth = ((curr - prev) / prev) * 100.0;
                        growthList.add(Math.round(growth * 10) / 10.0); // 保留一位小数
                    }
                }
            }

            industryMap.put(label, growthList);
        }

        Map<String, Object> response = new HashMap<>();
        response.put("year", years);
        for (Map.Entry<String, List<Double>> e : industryMap.entrySet()) {
            response.put(e.getKey(), e.getValue());
        }

        return response;
    }

    @ApiOperation("就业率最高的五个学院")
    @GetMapping("/chart/chart4")
    public List<ChartData> selectRate() {return chartService.selectRate();}

}

